@isTest
private class test {
	
	static List<User> usrList = new List<User>();
	static List<Profile> prflList = [select Id from Profile limit 1];
	static List<Group> grpList = new List<Group>();
	static List<GroupMember> gmList = new List<GroupMember>();
	static List<QueueSetting__c> qsList = new List<QueueSetting__c>();
	static List<Case> csList = new List<Case>();
	static List<Lead> ldList = new List<Lead>();
	static List<QueueSobject> qsobList = new List<QueueSobject>();
	static List<QueuePrioritizationRule__c> qprList = new List<QueuePrioritizationRule__c>();
	
	static void createTestData() {
		/** Create Custom settings */
		AssignmentForceSettings__c assSett = new AssignmentForceSettings__c();
        assSett.Name = 'Test Sett 1';
        assSett.ObjectAPIName__c = 'Case';
        assSett.ActiveFieldValues__c = 'New';
        assSett.ActiveFieldAPIName__c = 'Status';
        upsert assSett;
		
		/** Create users */
		User usr = new User();
		usr.LastName = 'Last1';
		usr.Username = 'lst@gmail.com';
		usr.Email = 'lst@gmail.com';
		usr.Alias = 'lst';
		usr.CommunityNickname = 'lst';
		usr.TimeZoneSidKey = 'Africa/Cairo';
		usr.LocaleSidKey = 'en_US';
		usr.EmailEncodingKey = 'ISO-8859-1';
		usr.ProfileId = prflList[0].Id;
		usr.LanguageLocaleKey = 'en_US';
		usr.Working_days__c = 'Sunday;Monday;Tuesday;Wednesday;Thurthday;Friday;Saturday';
		usrList.add(usr);
		
		usr = new User();
		usr.LastName = 'Last2';
		usr.Username = 'lst2@gmail.com';
		usr.Email = 'lst2@gmail.com';
		usr.Alias = 'lst2';
		usr.CommunityNickname = 'lst2';
		usr.TimeZoneSidKey = 'Africa/Cairo';
		usr.LocaleSidKey = 'en_US';
		usr.EmailEncodingKey = 'ISO-8859-1';
		usr.ProfileId = prflList[0].Id;
		usr.LanguageLocaleKey = 'en_US';
		usr.Working_days__c = 'Sunday;Monday;Tuesday;Wednesday;Thurthday;Friday;Saturday';
		usrList.add(usr);
		
		insert usrList;
		
		/** Create queue */
		Group grp = new Group();
		grp.Name = 'Group 1';
        grp.Type = 'Queue';
        grpList.add(grp);
        
		grp = new Group();
		grp.Name = 'Group 2';
        grp.Type = 'Queue';
        grpList.add(grp);
        
        insert grpList;
        
		/** Create queue sobject */
		QueueSobject qsob = new QueueSobject();
		qsob.SobjectType = 'Case';
        qsob.QueueId = grpList[0].Id;
        qsobList.add(qsob);
        
		qsob = new QueueSobject();
		qsob.SobjectType = 'Lead';
        qsob.QueueId = grpList[0].Id;
        qsobList.add(qsob);
        
        System.runAs(new User(Id = UserInfo.getUserId(),Working_days__c = 'Sunday;Monday;Tuesday;Wednesday;Thurthday;Friday;Saturday')) {
        	insert qsobList;
        }
        
        /** Creat queue setting */
        QueueSetting__c qs = new QueueSetting__c();
        qs.Name = 'Qs1';
        qs.AssignmentType__c = 'Round Robin';
        qs.Active__c = true;
        qs.QueueId__c = ((String)grpList[0].Id).substring(0,15);
        qsList.add(qs);
		
        qs = new QueueSetting__c();
        qs.Name = 'Qs1';
        qs.AssignmentType__c = 'Load Balanced';
        qs.Active__c = true;
        qs.QueueId__c = ((String)grpList[1].Id).substring(0,15);
        qsList.add(qs);
		
		System.runAs(new User(Id = UserInfo.getUserId(),Working_days__c = 'Sunday;Monday;Tuesday;Wednesday;Thurthday;Friday;Saturday')) {
			insert qsList;
		}
        
        /** Create group members */
        GroupMember gm = new GroupMember();
        gm.GroupId = grpList[0].Id;
        gm.UserOrGroupId = usrList[0].Id;
        gmList.add(gm);
        
        gm = new GroupMember();
        gm.GroupId = grpList[0].Id;
        gm.UserOrGroupId = usrList[1].Id;
        gmList.add(gm);
        System.runAs(new User(Id = UserInfo.getUserId(),Working_days__c = 'Sunday;Monday;Tuesday;Wednesday;Thurthday;Friday;Saturday')) {
        	insert gmList;
        }
        
        
        /** Create Priority Rules */
        QueuePrioritizationRule__c qpr = new QueuePrioritizationRule__c();
        qpr.QueueSettingId__c = qsList[0].Id;
        qpr.FieldAPIName__c = 'Status';
        qpr.ExecutionOrder__c = 2;
        qprList.add(qpr);
		
        qpr = new QueuePrioritizationRule__c();
        qpr.QueueSettingId__c = qsList[0].Id;
        qpr.FieldAPIName__c = 'Status';
        qpr.ExecutionOrder__c = 1;
        qprList.add(qpr);
		
        qpr = new QueuePrioritizationRule__c();
        qpr.QueueSettingId__c = qsList[1].Id;
        qpr.FieldAPIName__c = 'Status';
        qpr.ExecutionOrder__c = 1;
        qprList.add(qpr);
        
        insert qprList;
        
		/** Create Case and Leads */
        Lead l = new Lead();
        l.OwnerId = qsobList[1].QueueId;
        l.LastName = 'DS';
        l.Company = 'DS';
        ldList.add(l);
        upsert ldList;
        ldList[0].OwnerId = qsobList[1].QueueId;
        upsert ldList;
        
		Case c = new Case();
        c.OwnerId = ((String)qsobList[0].QueueId).substring(0,15);
        c.Status = 'New';
        c.Origin = 'Phone';
        csList.add(c);
		c = new Case();
        c.OwnerId = ((String)qsobList[0].QueueId).substring(0,15);
        c.Status = 'New';
        c.Origin = 'Phone';
        csList.add(c);
		c = new Case();
        c.OwnerId = ((String)qsobList[0].QueueId).substring(0,15);
        c.Status = 'New';
        c.Origin = 'Phone';
        csList.add(c);
        upsert csList;
        
		c = new Case();
        c.OwnerId = ((String)qsobList[0].QueueId).substring(0,15);
        c.Status = 'New';
        c.Origin = 'Phone';
        csList.add(c);
        upsert csList;
        
        
	}
	
	static testMethod void myUnitTest() {
		createTestData();
		ProcessRecords cntrl = new ProcessRecords(csList,true,false);
		cntrl.processObjects(csList,true);
		system.assertNotEquals(csList, null);
		
		ManualControl_controller manualController = new ManualControl_controller();
        manualController.runScheduler();
        system.assertNotEquals(manualController, null);
        
        ProcessRecords prController = new ProcessRecords(null,null,null);
        prController.start(null);
        prController.execute(null,null);
        prController.finish(null);
        system.assertNotEquals(prController, null);
	}
}